<?php

namespace app\admin\controller;

use think\App;
use think\Controller;
use app\common\model\DialogConfigModel;
use app\common\model\ConfigPubModel;
use think\facade\Request;

class DialogConfigController extends BaseController
{
    public function index()
    {
        return $this->fetch();
    }

    public function list()
    {
        $where = [];

        $title = Request::param('title');
        $status = Request::param("status");

        if ($title) {
            $where[] = ['title', 'like', "%{$title}%"];
        }
        if ($status != null) {
            $where[] = ['status', '=', $status];
        }
        $page  = Request::param("page");
        $limit = Request::param("limit");
        $list   = DialogConfigModel::where($where)->limit(($page - 1) * $limit, $limit)->order("id", "desc")->select();
        if (count($list) > 0) {
            $count = DialogConfigModel::where($where)->count("id");
            return json(["code" => 0, "msg" => "", "data" => $list, "count" => $count]);
        }
        return self::bulidFail("未查询到相关数据");
    }

    public function create()
    {
        return $this->fetch();
    }

    public function edit()
    {
        $id = Request::param("id");
        $data = DialogConfigModel::Where("id", $id)->find();
        $this->assign("data", $data);
        return $this->fetch();
    }

    public function save()
    {
        $params = Request::param();

        if (!empty($params['dialog_switch']) && is_array($params['dialog_switch'])) {
            $params['dialog_switch'] = array_sum($params['dialog_switch']);
        } else {
            $params['dialog_switch'] = 0;
        }

        $d = new DialogConfigModel($params);
        $d->created_at = date("Y-m-d H:i:s");

        if (isset($params['id']) && intval($params['id']) > 0) {
            $status = DialogConfigModel::get($params['id']);

            if ($status['status'] == 1) {
                DialogConfigModel::where('status', 1)->update(["status" => 2]); //tp强制更新所有

                // 更新公共配置接口表
                $config_pub = ConfigPubModel::get(1);
                $config_pub->dialog_title = $params['title'];
                $config_pub->dialog_timeout = $params['timeout'];
                $config_pub->dialog_force_reg = $params['force_reg'];
                $config_pub->dialog_daily_times = $params['daily_times'];
                $config_pub->dialog_web_image = $params['web_image'];
                $config_pub->dialog_mobile_image_h = $params['mobile_image_h'];
                $config_pub->dialog_mobile_image_v = $params['mobile_image_v'];
                $config_pub->dialog_switch = $params['dialog_switch'];
                $config_pub->save();
                redis_del('go_webapi_ConfigPub');
            }

            $params['status'] = $status['status'];
            $d->update($params);
            return parent::bulidSuccess();
        } else {
            $d->save();
            return parent::bulidSuccess();
        }

        return self::bulidFail();
    }

    public function status()
    {
        $id = Request::param("id");
        $status = Request::param("status");

        if ($status == 1) {
            $d = DialogConfigModel::get($id);
            // 更新公共配置接口表
            $config_pub = ConfigPubModel::get(1);
            $config_pub->dialog_title = $d->title;
            $config_pub->dialog_timeout = $d->timeout;
            $config_pub->dialog_force_reg = $d->force_reg;
            $config_pub->dialog_daily_times = $d->daily_times;
            $config_pub->dialog_web_image = $d->web_image;
            $config_pub->dialog_mobile_image_h = $d->mobile_image_h;
            $config_pub->dialog_mobile_image_v = $d->mobile_image_v;
            $config_pub->dialog_switch = $d->dialog_switch;
            $config_pub->save();

            DialogConfigModel::where('status', 1)->update(["status" => 2]); //tp强制更新所有
            if (DialogConfigModel::where("id", $id)->update(["status" => $status])) {
                redis_del('go_webapi_ConfigPub');
                return parent::bulidSuccess();
            }
        }
        if ($status == 2) {
            $config_pub = ConfigPubModel::get(1);
            $config_pub->dialog_switch = 0;
            $config_pub->save();
            if (DialogConfigModel::where("id", $id)->update(["status" => $status])) {
                redis_del('go_webapi_ConfigPub');
                return parent::bulidSuccess();
            }
        }
        return self::bulidFail();
    }

    public function delete()
    {
        $id = Request::param("id");
        if (DialogConfigModel::where("id", $id)->delete()) {
            return parent::bulidSuccess();
        }
        return self::bulidFail();
    }
}
